/*
 * LEDz-Clock
 * Version 1.0
 * Martin de Graaf
 */ 
 
#include "LedControl.h"
#include <Wire.h>
#include <DS1307.h> 

#define minutePin 5
#define hourPin 6
#define modePin 7

/* 
 * Create a new LedControl. 
 * pins 8,9 and 10  are used on the Arduino for the SPI interface
 * Pin 8 is connected to the DATA IN-pin of the first MAX7219
 * Pin 9 is connected to the CLK-pin of both MAX7219
 * Pin 10 is connected to the LOAD(/CS)-pin of both MAX7219 	
 * There will be two MAX7219 attached to the arduino 
 */
 
LedControl lc1=LedControl(8,9,10,2); 


// declare variables
int Ledpin = 13;
int DelayA = 50;
int second=0, minute=0, hour=0, hours=0; 
int ClockMode = 1;
int minutestate = 0;
int hourstate = 0;
int currenthour = 0;
int boot = 0;
//Previous clockmode
int PrevClockMode = 1;
//variables for mode change button
int modePushCounter = 0;   // counter for the number of button presses
int modeState = 0;         // current state of the button
int lastmodeState = 0;     // previous state of the button

//Minute and second array from 0 to 59. 
byte Pos1A[] = {7,7,7,7,7,7,7,6,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,4,4,4,4,4,4,4,4,3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,7};
byte Pos1B[] = {6,5,4,3,2,1,0,7,6,5,4,3,2,1,0,7,6,5,4,3,2,1,0,7,6,5,4,3,2,1,0,3,2,1,0,7,6,5,4,3,2,1,0,7,6,5,4,3,2,1,0,7,6,5,4,3,2,1,0,7};

//Hour array from 0 to 12. 
byte Pos2A[] = {0,0,1,1,1,1,0,0,0,0,0,0,0};
byte Pos2B[] = {1,0,3,2,1,0,7,6,5,4,3,2,1};

//Temperature array
//TODO
byte Pos3A[] = {};
byte Pos3B[] = {};


void setup() { 
   void clearDisplay(int addr);
  //wake up the MAX7219 from power-saving mode 
   lc1.shutdown(0,false);
   lc1.shutdown(1,false);
  //set brightness from 0 to 15 for the Leds
   lc1.setIntensity(0,0);
   lc1.setIntensity(1,1);
   
  pinMode(hourPin, INPUT);
  pinMode(minutePin, INPUT);  
  pinMode(minutePin, INPUT);  

  digitalWrite(minutePin, HIGH); //High turns on pull-up resistors
  digitalWrite(hourPin, HIGH);
  digitalWrite(modePin, HIGH);

  //Bootup  
  if(boot==0) {for (int minuteled=59; minuteled >=0; minuteled--) {

    delay(20);
    lc1.setLed(0,Pos1A[minuteled],Pos1B[minuteled],true);
    
    }

    if(boot==0) {for (int minuteled=0; minuteled <=59; minuteled++) {
    
    delay(20);
    lc1.setLed(0,Pos1A[minuteled],Pos1B[minuteled],false);}
    
    }
  
  }  

  boot=1;

  lc1.setIntensity(0,0);
  lc1.clearDisplay(0);
  lc1.clearDisplay(1);

  //Read currenthour
  currenthour = hours;

}

void loop () {

  //Clock mode change
  modeState = digitalRead(modePin);

  if (modeState != lastmodeState) {
    
    if (modeState == HIGH) {
      
      modePushCounter++;
      lc1.clearDisplay(0);
      lc1.clearDisplay(1);
    
    } 
    
    lastmodeState = modeState;
  }
  
  if (modePushCounter > 2) {modePushCounter=1;}
  
  //Read time from RTC
  hour = RTC.get(DS1307_HR,true);
  second = RTC.get(DS1307_SEC,false);
  minute = RTC.get(DS1307_MIN,false);
  hours = hour%12; //convert 24 hour to 12 hour

  //Set RTC
  setTime();  
 

  switch (modePushCounter) {
      case 1:
        ClockMode1();
      break;
      
      case 2:
        ClockMode2();
      break;
      
      default: 
         ClockMode1();
  }
      

}
